﻿using System;
using System.Text;
using System.Collections.Generic;
using NHibernate;
using Common;
using Educational.Model.Entity;
using Educational.Model.EntityManager;

namespace Educational.Business
{
    public class GradeAdmin
    {
        #region DeleteGrade删除一个年级
        /// <summary>
        /// 删除一个年级
        /// </summary>
        /// <param name="schoolId"></param>
        /// <param name="gradeId"></param>
        public static void DeleteGrade(int schoolId, int gradeId)
        {
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                Basic.Model.Entity.Collision collision = new Basic.Model.Entity.Collision();
                collision.itemKey = "DeleteGrade" + gradeId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision);

                Grade grade = GradeManager.Get2(session, gradeId);
                if (grade == null)
                    throw new Exception("当前系统不存在这个年级！");
                if (grade.school.schoolId != schoolId)
                    throw new Exception("你在执行一个你没有权限的操作。经过系统判断，你应该在试探攻击本系统，送你两个字：SB");
                if (grade.eclasses.Count > 0)
                    throw new Exception("当前年级下存在班级，请先移除班级！");
                GradeManager.Delete(session, grade);

                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion

        #region UpdateGrade修改一个年级
        /// <summary>
        /// 修改一个年级
        /// </summary>
        /// <param name="schoolId"></param>
        /// <param name="gradeId"></param>
        /// <param name="gradeName"></param>
        public static void UpdateGrade(int schoolId,int gradeId, string gradeName)
        {
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                Basic.Model.Entity.Collision collision = new Basic.Model.Entity.Collision();
                collision.itemKey = "DeleteSchool" + schoolId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision);
                Basic.Model.Entity.Collision collision1 = new Basic.Model.Entity.Collision();
                collision1.itemKey = "DeleteGrade" + gradeId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision1);

                Grade grade = GradeManager.Get1(session, gradeId);
                if (grade == null)
                    throw new Exception("当前系统不存在这个年级！");
                if (grade.school.schoolId != schoolId)
                    throw new Exception("你在执行一个你没有权限的操作。经过系统判断，你应该在试探攻击本系统，送你两个字：SB");
                grade.gradeName = gradeName;

                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision);
                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision1);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion

        #region AddGrade添加一个年级
        /// <summary>
        /// 添加一个年级
        /// </summary>
        /// <param name="schoolId"></param>
        /// <param name="gradeName"></param>
        public static void AddGrade(int schoolId, string gradeName)
        {
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                Basic.Model.Entity.Collision collision = new Basic.Model.Entity.Collision();
                collision.itemKey = "DeleteSchool" + schoolId.ToString();
                Basic.Model.EntityManager.CollisionManager.AddItem(session, collision);

                Grade grade = new Grade();
                grade.gradeName = gradeName;
                grade.school = SchoolManager.Get(session, schoolId);
                if (grade.school == null)
                    throw new Exception("当前系统不存在这个学校。");
                GradeManager.Add(session, grade);

                Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collision);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion

        #region GetsGrade查询所有年级
        /// <summary>
        /// 查询所有年级
        /// </summary>
        /// <param name="schoolId"></param>
        /// <param name="rowIndex"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public static PageResult<Educational.VO.Grade> GetsGrade(int schoolId, int rowIndex, int rowCount)
        {
            IList<Grade> entityData = null;
            PageResult<Educational.VO.Grade> pageResult = new PageResult<Educational.VO.Grade>();
            pageResult.data = new List<Educational.VO.Grade>();
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                entityData = GradeManager.Gets1(session, schoolId, rowIndex, rowCount);
                pageResult.totalCount = GradeManager.Gets1Count(session, schoolId);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
            for (int i = 0; i < entityData.Count; i++)
            {
                Educational.VO.Grade grade = new Educational.VO.Grade();
                grade.gradeId = entityData[i].gradeId;
                grade.gradeName = entityData[i].gradeName;
                pageResult.data.Add(grade);
            }
            return pageResult;
        }
        #endregion

        #region 获取所有年级的下拉列表HTML代码
        public static string GetGradeSelectHtmlString(int schoolId, string selectId, string selectName, int selectedId)
        {
            ISession session = null;
            ITransaction trans = null;
            StringBuilder html = new StringBuilder();
            IList<Grade> entityData = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                entityData = GradeManager.Gets2(session, schoolId);
                trans.Commit();

                html.AppendFormat("<select id=\"{0}\" name=\"{1}\" class=\"input\">", selectId, selectName);
                for (int i = 0; i < entityData.Count; i++)
                {
                    if (entityData[i].gradeId != selectedId)
                        html.AppendFormat("<option value=\"{0}\">{1}</option>", entityData[i].gradeId, entityData[i].gradeName);
                    else
                        html.AppendFormat("<option value=\"{0}\" selected=\"selected\">{1}</option>", entityData[i].gradeId, entityData[i].gradeName);
                }
                html.Append("</select>");
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                html.Append("读取年级下拉列表失败");
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
            return html.ToString();
        }
        #endregion
    }
}
